﻿@using KRM.Core
@{
    var db = (KRMEntities)ViewBag._db;
    var groupByID = (string)ViewBag.groupID;
    var orderItemsHeld = (List<KRMSP_OrderItemsHeld_Result>)ViewBag.orderItemsHeld;
}
<style>
    .row-selected td > a {
        color: #fff;
    }
</style>
<script>
    $(document).ready(function() {
        $('.table-grouping').dataTable({
            "iDisplayLength": 10000,
            "sScrollY": $('.table-grouping').attr("data-height"),
            "sScrollX": "100%",
            "aaSorting": [],
            "bSort": false
        });
        $(".dataTables_paginate").remove();
        InitTableSelected();
    })
</script>
<table class="table table-bordered table-fix tbl-selected table-grouping " data-height="450">
    <thead>
        <tr>
            <th>Action</th>
            <th>OrferRef</th>
            <th>WaitingAllocated</th>
            <th>Dateplaced</th>
            <th>DeliveryOption</th>
            <th>Deadlines</th>
            <th>ProductName</th>
            <th>ProductOption</th>
            <th>ProductSubOption</th>
            <th>Postcode</th>
            <th>ItemHistory</th>
            <th>ReasonText</th>
            <th>Title</th>
            <th>ForeName</th>
            <th>Sumane</th>
            <th>AddessLine1</th>
            <th>AddessLine2</th>
            <th>Town</th>
            <th>County</th>
            <th>Telephone1</th>
            <th>Telephone2</th>
            <th>ItemStatus</th>
        </tr>
    </thead>
    <tbody>
        @{var tg = "";}
        @foreach (var item in orderItemsHeld)
        {
            if (groupByID.Equals("ReasonText"))
            {
                if (!tg.Equals(item.reasonText))
                {
                    tg = item.reasonText;
            <tr>
                <td style="border-right: none; padding: 0">
                    <hr style="border-top: 1px groove #e3e3e3" />
                </td>
                <td style="border-left: none; padding: 0">
                    <div style="float: left; padding: 9px 4px; color: blue">@item.reasonText </div>
                    <hr style="border-top: 1px groove #e3e3e3" />
                </td>
                @for (int i = 0; i < 20; i++)
                {
                                                        
                    <td style="border-left: none; padding: 0">
                        <hr style="border-top: 1px groove #e3e3e3" />
                    </td> 
                }
            </tr>
                }
            }
            else
            {
                if (!tg.Equals(item.OrderRef))
                {
                    tg = item.OrderRef;
            <tr>
                <td style="border-right: none; padding: 0">
                    <hr style="border-top: 1px groove #e3e3e3" />
                </td>
                <td colspan="1" style="border-left: none; padding: 0">
                    <div style="float: left; padding: 9px 4px; color: blue">@item.OrderRef </div>
                    <hr style="border-top: 1px groove #e3e3e3" />
                </td>
                @for (int i = 0; i < 20; i++)
                {
                    <td style="border-left: none; padding: 0">
                        <hr style="border-top: 1px groove #e3e3e3" />
                    </td> 
                }
            </tr>
                }
            }
            <tr id="tr-@item.OrderItemID">
                <td onclick="HeldOrderSelected('@item.OrderItemID')">
                    <div class="btn-group">
                        <a href="#" data-toggle="dropdown" class="btn dropdown-toggle"><i class="icon-cog"></i><span class="caret"></span></a>
                        <ul class="dropdown-menu dropdowm-right">
                            <li>
                                <a href="/orderview/orderdetail?orderref=@item.OrderRef" target="_blank">View Order '@item.OrderRef'</a>
                            </li>
                            <li>
                                <a href="javascript:;"onclick="PopupCenter('/Stockcontrol/AmendAddressOn?OrderID=@item.OrderID&orderref=@item.OrderRef', 800, 500)">Amend Address on '@item.OrderRef'</a>
                            </li>
                            @{
            string itemHistory = item.ItemHistory;
            string buttonText = "";
            bool setExtraDeliveryCostToolStripMenuItemVisible;
            var setExtraDeliveryCostToolStripMenuItemEnabled = true;
            var releaseItemToolStripMenuItemVisible = false;
            if (item.reasonText.ToLower() == "postcode alert for carrier raised")
            {
                setExtraDeliveryCostToolStripMenuItemVisible = true;
                setExtraDeliveryCostToolStripMenuItemEnabled = true;
                if (itemHistory.IndexOf("Extra Delivery Cost: |") != -1 && itemHistory.IndexOf("Delivery Cost Payment Taken") == -1)
                {
                    int indexofDeliveryCost = itemHistory.IndexOf("Extra Delivery Cost: |") + 22;
                    int indexEndofDeliveryCost = itemHistory.IndexOf("| Assigned by (");
                    buttonText = "Take extra payment of " + itemHistory.Substring(indexofDeliveryCost, indexEndofDeliveryCost - indexofDeliveryCost);
                }
                else if (itemHistory.IndexOf("Extra Delivery Cost: |") == -1)
                {
                    buttonText = "Set Extra Delivery Cost";
                }
                else
                {
                    buttonText = "There may be an error, extra payment has been taken";
                    setExtraDeliveryCostToolStripMenuItemEnabled = false;
                }
            }
            else if (item.reasonText.ToLower() == "new order not yet checked" && item.ItemStatus > 2)
            {
                setExtraDeliveryCostToolStripMenuItemVisible = false;
                releaseItemToolStripMenuItemVisible = true;
            }
            else
            {
                setExtraDeliveryCostToolStripMenuItemVisible = false;
            }
            var sentstocks = db.tblKRMSentStocks.Where(a => a.OrderRef == item.OrderRef);

            var markedOrderToFixToolStripMenuItemEnable = ((sentstocks.Count() == 0) && item.WaitingAllocated != true);

            if (setExtraDeliveryCostToolStripMenuItemVisible)
            {
                if (setExtraDeliveryCostToolStripMenuItemEnabled)
                {
                                <li>

                                    <a href="javascript:;" onclick="SetExtraDeliveryCost('@buttonText','@item.OrderID','@item.Postcode','@item.OrderItemID')">@buttonText</a>
                                </li>
                }
                else
                {
                                <li>
                                    <a style="color: #999" href="javascript:;">@buttonText</a>
                                </li>
                }
            }
            if (releaseItemToolStripMenuItemVisible)
            {
                                <li>
                                    <a href="javascript:;" onclick="ReleaseItem('@item.OrderItemID','@item.OrderRef','')">Release Item</a>
                                </li>
            }
            if (markedOrderToFixToolStripMenuItemEnable)
            {
                                <li>
                                    <a href="javascript:;" onclick="MarkedOrderToFix('@item.OrderID')">Marked Order to Fix</a>
                                </li>
            }
            else
            {
                                <li>
                                    <a href="javascript:;" style="color: #999">Marked Order to Fix</a>
                                </li>
            }
                            }

                        </ul>
                    </div>
                </td>
                <td><a href="/orderview/orderdetail?orderref=@item.OrderRef" target="_blank" >@item.OrderRef</a></td>
                <td>@item.WaitingAllocated</td>
                <td>@item.dateplaced</td>
                <td>@item.DeliveryOption</td>
                <td>@item.Deadlines</td>
                <td>@item.ProductName</td>
                <td>@item.ProductOption</td>
                <td>@item.ProductSubOptions</td>
                <td>@item.Postcode</td>
                <td>@item.ItemHistory</td>
                <td>@item.reasonText</td>
                <td>@item.Title</td>
                <td>@item.Forename</td>
                <td>@item.Surname</td>
                <td>@item.AddressLine1</td>
                <td>@item.AddressLine2</td>
                <td>@item.Town</td>
                <td>@item.County</td>
                <td>@item.Telephone1</td>
                <td>@item.Telephone2</td>
                <td>@item.ItemStatus</td>
            </tr>
        }
    </tbody>
</table>
